<?php
/**
 * Created by PhpStorm.
 * 视图层渲染类
 * User: gxk
 * Date: 2019/4/15
 * Time: 10:05
 */

namespace Library;
use Slim\Views\PhpRenderer;
class View
{
    //模板类
    protected static $view;
    //response类
    protected $response;
    public function __construct()
    {
        //实例化模板类,设置默认文件夹
//        if(is_null(self::$view)){
//            self::$view = new PhpRenderer(BASE_PATH_VIEW.'/index/');
//        }
    }
    /*
     * 取得view类实例
     */
    public function getObjView()
    {
        if(is_null(self::$view)){
            self::$view = new PhpRenderer(BASE_PATH_VIEW.'/index/');
        }
    }
    /*
     * 设置默认文件夹
     * @param string $path 路径
     */
    public function setDirView($path)
    {
        self::getObjView();
        self::$view->setTemplatePath($path);
    }
    /*
     *
     */
    public function fetch($data=[],$template='')
    {
        self::getObjView();
        $referInfo = debug_backtrace();
        $backInfo = $referInfo[1];
        if(empty($template)){
            $template = $backInfo['function'].'.php';
        }
        self::$view->fetch($template,$data);
    }
    /*
     * 展示模板并赋值
     * @param string $dirname 文件
     * @param array $data 数据
     */
    public function render($data=[],$dirname='',$response=null)
    {
        self::getObjView();
        $referInfo = debug_backtrace();
        $backInfo = $referInfo[1];
        if(empty($dirname)){
            $dirname = $backInfo['function'].'.php';
        }else{
            $dirname = $dirname.'.php';
        }
        if(empty($response)){
            $response = $backInfo['args'][1];
        }
        self::$view->render($response,$dirname,$data);
    }
}